// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.viewers.excel;

import jxl.Cell;
import jxl.Sheet;

import org.dengues.commons.utils.FileUtils;
import org.dengues.core.warehouse.ENodeCategoryName;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.warehouse.viewers.wizards.NewElementWizardPage2;
import org.eclipse.core.runtime.IPath;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-7-4 qiang.zhang $
 * 
 */
public class NewExcelWizardPage2 extends NewElementWizardPage2 {

    private CTabFolder folder;

    /**
     * Qiang.Zhang.Adolf@gmail.com NewCSVWizardPage2 constructor comment.
     * 
     * @param path
     * @param database
     */
    public NewExcelWizardPage2(IPath path, DatabaseDiagram database, ENodeCategoryName name) {
        super(path, database, name);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.warehouse.viewers.wizards.NewElementWizardPage2#createControl(org.eclipse.swt.widgets.Composite)
     */
    @Override
    public void createControl(Composite parent) {
        super.createControl(parent);
        folder = new CTabFolder(container, SWT.BORDER);
        folder.setLayout(new GridLayout());
        folder.setLayoutData(new GridData(GridData.FILL_BOTH));
        if (database != null) {
            databaseName = (String) database.getProperties().get("FILENAME");
            setFileName(databaseName);
            setPageComplete(true);
        } else {
            setPageComplete(false);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.warehouse.viewers.wizards.NewElementWizardPage2#setFileName(java.lang.String)
     */
    @Override
    protected void setFileName(String open) {
        if (open != null) {
            nameText.setText(FileUtils.getPortablePath(open));
            clearFolder();
            try {
                jxl.WorkbookSettings settings = new jxl.WorkbookSettings();
                settings.setEncoding("UTF-8");
                jxl.Workbook rwb = jxl.Workbook.getWorkbook(new java.io.File(open), settings);
                Sheet[] sheets = rwb.getSheets();
                for (Sheet sheet : sheets) {
                    createTabItem(sheet);
                }
            } catch (Exception e1) {
                // do nothings
            }
            folder.layout(true);
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createTabItem".
     * 
     * @param sheet
     */
    private void createTabItem(Sheet sheet) {
        CTabItem item = new CTabItem(folder, SWT.NONE);
        item.setText(sheet.getName());
        Table table = new Table(folder, SWT.BORDER | SWT.FULL_SELECTION);
        table.setLinesVisible(true);

        int columns = sheet.getColumns();
        for (int i = 0; i < columns; i++) {
            TableColumn column = new TableColumn(table, SWT.NONE);
            column.setWidth(50);
        }
        String[] row = new String[columns];
        int rows = sheet.getRows();
        for (int i = 0; i < (rows > 15 ? 15 : rows); i++) {
            Cell[] cells = sheet.getRow(i);
            for (int j = 0; j < cells.length; j++) {
                Cell cell2 = cells[j];
                String text = cell2.getContents();
                row[j] = text;
            }
            TableItem tableItem = new TableItem(table, SWT.NONE);
            tableItem.setText(row);
        }
        if (folder.getItemCount() > 0) {
            folder.setSelection(0);
        }
        if (table.getItemCount() > 0) {
            table.setSelection(0);
        }
        item.setControl(table);

    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "clearFolder".
     */
    private void clearFolder() {
        if (folder != null && !folder.isDisposed()) {
            Control[] children = folder.getChildren();
            for (Control control : children) {
                control.dispose();
            }
            CTabItem[] items = folder.getItems();
            for (CTabItem tabItem : items) {
                tabItem.dispose();
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.warehouse.viewers.wizards.NewElementWizardPage2#canFlipToNextPage()
     */
    @Override
    public boolean canFlipToNextPage() {
        return super.canFlipToNextPage();
    }

    /**
     * Getter for folder.
     * 
     * @return the folder
     */
    public CTabFolder getFolder() {
        return this.folder;
    }

}
